import api from '@/api';

export default {
  namespace: 'infoenterprise',

  state: {
    enterpriseInfo: {},
    basicInfo: {},
    isEdit: false,
    stepNumber: 0
  },

  effects: {
    *fectchInfo({ payload }, { call, put }){
        const response = yield call(api.info.queryEnterpriseInfo, payload);
        console.log('response', response)
        if (response.code === 200) {
            yield put({
              type: 'save',
              payload: response.data
            })
        }
    }, 
    *updateInfo({ payload }, { call, put }){
      return yield call(api.info.modifyEnterpriseInfo, payload)
    },
    *uploadImage({ payload }, { call, put }){
      return yield call(api.info.uploadImage, payload)
    },
    *getCountryList({ payload }, { call, put }){
      return yield call(api.info.getCountryList, payload)
    },
    *getCityList({ payload }, {call, put}){
      return yield call(api.info.getCityList, payload)
    },
    *updateRate({payload}, {call, put}){
      return yield call(api.info.updateRate, payload)
    },
    *enterpriseSubmit({payload}, {call, put}){
      return yield call(api.info.enterpriseSubmit, payload)
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        enterpriseInfo: action.payload
      }
    },
    toggleEdit(state, action){
      return {
        ...state,
        isEdit: !state.isEdit
      }
    },
    changeStepNumber(state, action){
      return {
        ...state,
        stepNumber: action.payload.data
      }
    },
    modifyEnterpriseInfo(state, action){
      return {
        ...state,
        enterpriseInfo: {...state.enterpriseInfo, ...action.payload},
        basicInfo: {...action.payload}
      }
    },
    modifyServiceRate(state, action){
      let arr = JSON.parse(JSON.stringify(state.enterpriseInfo.spsRateProfitResponses))
      arr[0] = {...arr[0], ...action.payload}
      return {
        ...state,
        enterpriseInfo: {...state.enterpriseInfo, spsRateProfitResponses: arr}
      }
    },
    modifyServiceInfo(state, action){
      console.log('modify service info', state.enterpriseInfo)
      return {
        ...state,
        enterpriseInfo: {...state.enterpriseInfo, ...action.payload},
      }
    }
  }
};
